import QtQuick 2.6
import QtQuick.Layouts 1.1

import "../../Base/Config"
import Data 1.0
import Code 1.0

import Cpp.Type 1.0
import Cpp.Data 1.0 as CppData
import Cpp.Request 1.0 as Request

import  "../../FallView" as FallView
import "./Post" as Post

Item {
    ColumnLayout {
        anchors.fill: parent
        spacing: Size.elementInterval

        Header {
            id: header
            Layout.fillWidth: true
            Layout.preferredHeight: height
        }

        Item {

            Layout.fillWidth: true
            Layout.fillHeight: true
            z: -1

            FallView.FallView {
                id:flickable
                anchors.fill: parent

                contentHeight: contentItem.childrenRect.height
                               + (200 * Size.ratio)*(moving||postFetcher.status === Request.MultiPostFetcher.Communicating)

                delegate: Post.Post {
                    width: parent.width

                    postId: model.postId
                    name: model.name
                    headSource: model.head
                    subject: model.subject
                    time: model.time
                    province: model.province
                    city: model.city
                    imageCount: model.imageCount
                    imageSource: model.image
                    audioSource: model.audio
                    comment: model.comment
                    type: model.type
                }
                model: listmodel
            }

            Rectangle {
                property Flickable target: flickable
                property double floatingY: -target.contentY - height
                property double fixedY: 0
                property bool fullyVisible: y === fixedY

                Component.onCompleted: {
                    target.movingChanged.connect(onTargetMovingChanged)
                }

                function onTargetMovingChanged()
                {
                    if(!target.moving && fullyVisible && target.arrangeStatus === "Ok" && postFetcher.status !== Request.MultiPostFetcher.Communicationing)
                        postFetcher.autoFetchNewer()
                }

                visible: y > -height
                width: target.width
                height: (target.contentHeight - target.contentItem.childrenRect.height) / 2
                y : {
                    if(floatingY < 0)
                        return floatingY
                    return fixedY
                }

                color: Qt.rgba(1, 0, 0, 0.05)
            }

            Rectangle {
                property Flickable target: flickable
                property double floatingY: -target.contentY + target.contentItem.childrenRect.height
                property double fixedY: target.height - height
                property bool fullyVisible: y === fixedY

                Component.onCompleted: {
                    target.movingChanged.connect(onTargetMovingChanged)
                }

                function onTargetMovingChanged()
                {
                    if(!target.moving && fullyVisible && target.arrangeStatus === "Ok" && postFetcher.status !== Request.MultiPostFetcher.Communicationing)
                        postFetcher.autoFetchOlder()
                }

                visible: y < target.height
                width: target.width
                height: (target.contentHeight - target.contentItem.childrenRect.height) / 2
                y : {
                    if(floatingY + height > target.height)
                        return floatingY
                    return fixedY
                }

                color: Qt.rgba(1, 0, 0, 0.05)
            }
        }
    }

    Footer {
        width: parent.width
        anchors.bottom: parent.bottom

        subject: User.subject
    }


    ListModel {
        id: listmodel

        Component.onCompleted: {
            listmodel.append({
                                 postId: 1000,
                                 type: "钢琴  ",
                                 name: "欢乐的0403",
                                 head: "../../../resources/tests/touxiang02.svg",
                                 subject: Subject.Music,
                                 time: new Date("2016-03-27T18:00:00+08:00"),
                                 province: qsTr("广西"),
                                 city: qsTr("来宾"),
                                 audio: "../../../resources/tests/钢琴曲 - Summer.mp3",
                                 imageCount: 0,
                                 image: "",
                                 comment: "asdfasdf!@#!@#!@########!!!!!!!!!!!!!!!!!!"
                             })
            listmodel.append({
                                 postId: 10001,
                                 type: "就是画画  ",
                                 name: "欢乐的0403",
                                 head: "../../../resources/tests/touxiang02.svg",
                                 subject: Subject.Paint,
                                 time: new Date("2016-03-27T18:00:00+08:00"),
                                 province: qsTr("广西"),
                                 city: qsTr("来宾"),
                                 audio: "",
                                 imageCount: 1,
                                 image: "../../../resources/tests/121025539394.jpg",
                                 comment: "18:00"
                             })
            listmodel.append({
                                 postId: 10002,
                                 type: "再来一张画画  ",
                                 name: "欢乐的0403",
                                 head: "../../../resources/tests/touxiang02.svg",
                                 subject: Subject.Paint,
                                 time: new Date("2016-03-27T18:00:00+08:00"),
                                 province: qsTr("广西"),
                                 city: qsTr("来宾"),
                                 audio: "",
                                 imageCount: 1,
                                 image: "../../../resources/tests/221ABV2-7.jpg",
                                 comment: "asdfasdf!@#!@#!@########!!!!!!!!!!!!!!!!!!"
                             })
            listmodel.append({
                                 postId: 10003,
                                 type: "就是插图  ",
                                 name: "欢乐的0403",
                                 head: "../../../resources/tests/touxiang02.svg",
                                 subject: Subject.Pe,
                                 time: new Date("2016-03-27T18:00:00+08:00"),
                                 province: qsTr("广西"),
                                 city: qsTr("来宾"),
                                 audio: "",
                                 imageCount: 1,
                                 image: "../../../resources/tests/355X200.jpg",
                                 comment: "asdfasdf!@#!@#!@########!!!!!!!!!!!!!!!!!!"
                             })

        }
    }
    Request.MultiPostFetcher {
        id:postFetcher

        property date newestTime:new Date("2016-03-27T18:00:00+08:00")
        property date mostAncientTime:new Date("2016-03-27T18:00:00+08:00")

        function autoFetchNewer()
        {
            fetchNewer(newestTime, 1)
        }

        function autoFetchOlder()
        {
            fetchOlder(mostAncientTime, 1)
        }

        onOk: {
            if(posts.length > 0)
            {
                var i
                var time
                if(newestTime <= posts[0].time)
                {
                    for(i=0; i < posts.length; i++)
                    {
                        time = posts[i].time
                        newestTime = time > newestTime > 0 ? time : newestTime
                        listmodel.insert(0, posts[i])
                    }
                }
                else
                {
                    for(i=0; i < posts.length; i++)
                    {
                        time = posts[i].time
                        mostAncientTime = time < mostAncientTime ? time : mostAncientTime

                        listmodel.append(posts[i])
                    }
                }
            }

        }

    }
}
